<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Admin\Controller\AdminController;

class AuditController extends AdminController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'store'); // 数据集
    }
    // 店铺审核
    public function index(){
        if(IS_POST){
            $company_name = I('user_name');
            $data['company_name'] = array('like',"%$company_name%");
        }
        $store_joinin = M('store_joinin'); // 实例化User对象
        $count      = $store_joinin->where()->order('id desc')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
    // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $store_joinin->where($data)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('action', 'audit');
        $this->display(); // 输出模板
    }

    // 详情页
    public function details(){
        $data['user_id'] = I('get.user_id');
        $store_joinin = M('store_joinin');
        $res = $store_joinin->where($data)->find();
        $this->assign('res',$res);
        if(IS_POST){
            $datas['joinin_state'] = I('post.joinin_state');
            $res = $store_joinin->where($data)->save($datas);
            if($res && $datas['joinin_state'] == 40){
                $store = M('store');
                $arr = $store_joinin->where($data)->find();
                $data['store_name']          = $arr['store_name'];   //店铺名称
                $data['member_id']           = $arr['user_id'];    //店主id
                $data['member_name']         = $arr['user_name'];    //店主账号
                $data['store_company_name']  = $arr['company_name'];  //公司名称
                $data['province_id']         = $arr['company_province_id'];  //省份id
                $data['area_info']           = $arr['company_address'];  //省市县 汉字名称
                $data['store_address']       = $arr['company_address_detail'];  //详细地址
                $data['store_time']          = time();
                $re = $store->add($data);
                if(!$re){
                    $this->success('失败稍后重试',U('index'));
                }
                $da['user_store_id'] = $store->getLastInsID('store_id');
                $da['user_group_id'] = 2;
                $where['user_id'] = session('user_id');
                $re = M('users')->where($where)->data($da)->save();

                $re ? $this->success('批准通过审核',U('index')) : $this->error('失败，请从新审核');

            }
            $this->success('审批为不通过',U('index'));
        }

        $this->display();
    }

    // 删除
    public function delete(){
        $user_id = I('post.data');
        $store_joinin = M('store_joinin');
        $res = $store_joinin->delete($user_id);
        if($res){
                //日志记录
                $logobj = new Logs();
                $html =  '用户ID：'.$user_id.'的店铺';
                $logobj->write(array("管理员:".session('manager_auth.username'),"删除店铺",$html));
                
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败');
        }
    }

    //ajax修改状态
    public function state(){
        $p = I('post.');
        switch($p['type']){
            case '√':$data['joinin_state'] = 10;break;
            case '×':$data['joinin_state'] = 20;break;
        }
        $re = M('store_joinin')->where(array('id'=>$p['id']))->data($data)->save();
        if($re){
            $a['p'] = true;
        }else{
            $a['p'] = false;
        }
        $this->ajaxReturn($a);

    }
}
